﻿@model ElectroSystem.Models.ManageReturnsModel

@{string randomNum = ViewBag.randomNum;}
@{int pId = ViewBag.pId;}
@{Guid oId = ViewBag.oId;}
@{List<SelectListItem> faultList = ViewBag.faultList;}

@{
    ViewBag.Title = "Report Fault for a Product";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Report Fault for a Product</h2>
<script type="text/javascript">
    function checkEmpty() {
        if ($("#faultDetails").val() == "") {
            alert("Enter the fault details to report the fault!");
        }
        else {
            var ranNum = $('#randNo').val();
            var faultCat = $('#ddlFaultCat option:selected').val();
            var oID = $('#orID').val();
            var pID = $('#prodID').val();
            var faultDet = $('#faultDetails').val();
            var param = { randomNumber: ranNum, FaultCategory: faultCat, OrderID: oID, ProductID: pID, FaultDetails: faultDet };
            $.ajax({
                url: '@Url.Action("FaultDetails")',
                type: "POST",
                datatype: "json",
                data: param, // Passing the parameters var
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert("An error has occurred.");
                },
                success: function (data) {
                    alert("The Fault Details have been reported. Thank you");
                    $('#faultDetails').val("");
                    $('#ddlFaultCat option:selected').val("");
                }
            });
        }
    }
</script>
@using (Html.BeginForm())
{
    <fieldset>
        <legend>Enter fault details</legend>

        <div style="visibility:hidden">
            @Model.ProductID = @pId  
            @Html.HiddenFor(m => m.ProductID, new { id = "prodID" })

            @Model.OrderID = @oId
            @Html.HiddenFor(m => m.OrderID, new { id = "orID" })

            @Model.RandomNumber = @randomNum
            @Html.HiddenFor(m => m.RandomNumber, new { id = "randNo" })
        </div>
        
        <div class="editor-label">
            <b>@Html.LabelFor(model => model.FaultCategory, "Fault Category")</b>
        </div>

        <div class="editor-field">
            @Html.DropDownListFor(model => model.FaultCategory, faultList, new { id = "ddlFaultCat" })
            @Html.ValidationMessageFor(model => model.FaultCategory)
        </div>

        <div class="editor-label">
            <b>@Html.LabelFor(model => model.FaultDetails, "Fault Details")</b>
        </div>

        <div class="editor-field">
            @Html.TextAreaFor(model => model.FaultDetails, new { id = "faultDetails" })
            @Html.ValidationMessageFor(model => model.FaultDetails)
        </div>

        <p>
          <button type="button" onclick="checkEmpty()">Submit Fault</button>
        </p>
    </fieldset>
    
    
}
